package com.cn.star.java8;
/*
 * 两个线程交替累加
 */
public class T1 {
	Object lock = new Object();
    int x = 0;

	void test() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (lock) {
					while (x < 100) {
						lock.notifyAll();
						x = x + 1;
						System.out.println("线程一："+x);
						try {
							lock.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}).start();
		new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (lock) {
					while (x < 100) {
						lock.notifyAll();
						x = x + 1;
						System.out.println("线程二："+x);
						try {
							lock.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}).start();
	}

	public static void main(String[] args) {
		new T1().test();
	}

}
